<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    function microtask(task) {
        if (typeof Promise === "function") {
            Promise.resolve().then(task)
        }
        else if (typeof MutationObserver === "function") {
            const ob = new MutationObserver(task)
            const textNode = document.createTextNode("0")
            ob.observe(textNode, {
                // 监听文本数据
                characterData: true
            })
            textNode.data = "1"
        }
        // 兼容node环境
        else if (process && typeof process.nextTick === "function") {
            process.nextTick(task)
        }
        else {
            // 最后降级到setTimeout，虽然它是一个宏任务
            setTimeout(task, 0)
        }
    }
</script>

</html>